﻿using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Utils;



namespace ModuleGlobalMessage.Util
{
    /// <summary>
    /// 
    /// </summary>
    public class MessageBox
    {
        /// <summary>
        /// 缓存消息信息
        /// </summary>
        public static Queue<string> MessageCache;

        /// <summary>
        /// 展示用文本
        /// </summary>
        public static List<ItemShowBox> ShowBoxCache;

        /// <summary>
        /// 缓存池
        /// </summary>
        public static Queue<ItemShowBox> ItemBoxQueue;

        /// <summary>
        /// 最大展示行数
        /// </summary>
        public static uint MaxLine = 4;

        /// <summary>
        /// 增加消息栏目
        /// </summary>
        /// <param name="message"></param>
        public static void Queue(string message)
        {
            MessageCache.Enqueue(message);
        }

        /// <summary>
        /// 
        /// </summary>
        public static Coroutine BoxCOR;

        /// <summary>
        /// 
        /// </summary>
        public static void Enable()
        {
            ShowBoxCache = new List<ItemShowBox>();
            MessageCache = new Queue<string>();

            ItemBoxQueue = new Queue<ItemShowBox>();
            {
                for (int i = 0; i < MaxLine * 10; i++)
                {
                    ItemBoxQueue.Enqueue(new ItemShowBox(string.Empty));
                }
            }

            TimeCoroutine.Stop(BoxCOR);


            BoxCOR = TimeCoroutine.Loop(-1, 1, () =>
            {
                for (int i = 0; i < ShowBoxCache.Count; i++)
                {
                    ShowBoxCache[i].TimeDown();

                    if (ShowBoxCache[i].showTime == 0)
                    {
                        ItemBoxQueue.Enqueue(ShowBoxCache[0]);
                        ShowBoxCache.RemoveAt(0);
                    }
                }
                for (int i = 0; i < MessageCache.Count; i++)
                {
                    string messsage = MessageCache.Dequeue();
                    messsage = string.IsNullOrEmpty(messsage) ? string.Empty : messsage;

                    ItemShowBox item = ItemBoxQueue.Dequeue();
                    item.Set(messsage);

                    DoAppend(item);
                }
            });
        }


        public static void Dispose()
        {
            ShowBoxCache = null;
            MessageCache = null;
            ItemBoxQueue = null;

            TimeCoroutine.Stop(BoxCOR);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        private static void DoAppend(ItemShowBox item)
        {
            if (ShowBoxCache.Count > MaxLine)
            {
                ItemBoxQueue.Enqueue(ShowBoxCache[0]);
                ShowBoxCache.RemoveAt(0);
            }
        
            ShowBoxCache.Add(item);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ToMessage()
        {
            StringBuilder build = new StringBuilder();

            foreach (var item in ShowBoxCache)
            {
                item.CheckTime();
                
                build.AppendLine($"<b>[{item.showTime}] {item.showContent}</b>");
            }


            if (ShowBoxCache.Count < MaxLine)
            {
                for (int i = ShowBoxCache.Count; i < MaxLine; i++)
                {
                    build.AppendLine("");
                }
            }



            return build.ToString();
        }
    }
}
